package com.zretc.controller;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import com.zretc.service.AdminService;
import com.zretc.service.impl.AdminServiceImpl;

/**
 * Servlet implementation class AdminServlet
 */
@WebServlet("/AdminServlet")
public class AdminServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private AdminService adminService = new AdminServiceImpl();    
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AdminServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String op = request.getParameter("op");
		if("login".equals(op)) {
			doLogin(request,response);
		}else if("logout".equals(op)) {
			doLogout(request,response);
		}
	}
	private void doLogout(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 清除session信息
		HttpSession session = request.getSession();
		session.removeAttribute("adminMap");
		// 重定向
		response.sendRedirect("back/login.jsp");
	}
	private void doLogin(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String adminId = request.getParameter("adminId");
		String password = request.getParameter("password");
		Map<String, String> adminMap = adminService.login(adminId, password);
		if (adminMap != null) { 
			// 登录成功
			// 用户信息存储在session会话作用域中
			HttpSession session = request.getSession();
			session.setAttribute("adminMap", adminMap);
			
			// 获取复选框组件的值
			String rememberPassword = request.getParameter("rememberPassword");

			// 用户选择记住密码
			if(rememberPassword != null && "rememberPassword".equals(rememberPassword)) {
				// 通过 Servlet 添加 Cookie
				// 用户信息存储在cookie中,发送给HTTP响应头中
				// 单用户操作
				/*Cookie cookie1 = new Cookie("adminId", adminId);
				Cookie cookie2  = new Cookie("password", password);
				
				// 设置cookie的有效时间
				cookie1.setMaxAge(60*5);
				cookie2.setMaxAge(60*5);
				
				// 将cookie对象添加到HTTP响应头中
				response.addCookie(cookie1);
				response.addCookie(cookie2);
				*/
				
				/*多用户操作： name-用户名 value-密码*/
				Cookie cookie  = new Cookie(adminId, password);
				response.addCookie(cookie);
				
			}else {
				// 单用户操作
				// 用户没有选择记住密码
				// 通过 Servlet 删除 Cookie
				// 1读取一个现有的 cookie，并把它存储在 Cookie 对象中。
				// 2使用 setMaxAge() 方法设置 cookie 的年龄为零，来删除现有的 cookie。
				// 3把这个 cookie 添加到响应头。
			/*	Cookie[] cookies = request.getCookies();
				Cookie cookie = null;
				for (int i = 0; i < cookies.length; i++) {
					cookie = cookies[i];
					if(cookie.getName().equals("adminId")) {
						cookie.setMaxAge(0);
						response.addCookie(cookie);
					}
					if(cookie.getName().equals("password")) {
						cookie.setMaxAge(0);
						response.addCookie(cookie);
					}
				}
			*/
				// 多用户操作,用户没有选择记住密码,删除现有的cookie
				Cookie[] cookies = request.getCookies();
				Cookie cookie = null;
				for (int i = 0; i < cookies.length; i++) {
					cookie = cookies[i];
					if(cookie.getName().equals(adminId)) { // 用户名就是cookie的name
						cookie.setMaxAge(0);
						response.addCookie(cookie);
					}
				}
			}
			//  重定向到首页
			response.sendRedirect("back/index.jsp");
		}else {
			// 登录失败 
			// 重定向到登录页面
			response.sendRedirect("back/login.jsp?loginErrorMessage=error");
		}
	}

}
